import { fetchTopList } from '@/api/category'
import { topCategory } from '@/api/constants'

export default {
  namespaced: true,
  state () {
    return {
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    show (state, item) {
      // 根据item(传入的数据),id查找到对应的一级分类,改变open字段
      const category = state.list.find(category => category.id === item.id)
      category.open = true
    },
    hide (state, item) {
      // 根据item(传入的数据),id查找到对应的一级分类,改变open字段
      const category = state.list.find(category => category.id === item.id)
      category.open = false
    }
  },
  actions: {
    async getList (store) {
      const { result } = await fetchTopList()
      result.forEach(item => { item.open = false })
      store.commit('setList', result)
    }
  }
}
